package com.ta.lecture;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.EditText;

public class SharedPreference extends Activity {
	static final String PREF_USEVALUE = "PREF_USEVALUE";
 	EditText mEdit;
 	CheckBox mChk;
 	
 	@Override
 	protected void onCreate(Bundle savedInstanceState) {
	 	super.onCreate(savedInstanceState);
	 	setContentView(R.layout.activity_sharedpreference);
	 	Log.d("ta", "create");
	 	mEdit = (EditText) findViewById(R.id.edit);
	 	mChk = (CheckBox) findViewById(R.id.checkBox1);
 	}
 	
 	@Override
 	protected void onResume() {
 		// TODO Auto-generated method stub
 		SharedPreferences pref = getSharedPreferences("setting", MODE_PRIVATE);
		boolean checkbox = pref.getBoolean("PREF_CHECKBOX", false);
		String usevalue = pref.getString(PREF_USEVALUE, null);
		
		if(checkbox) {
			mChk.setChecked(true);
		}
		
		if(usevalue != null) {
			mEdit.setText(usevalue.toString());
		}
		
		Log.d("TA", "useJs" + checkbox + "/username" + usevalue);
 		
 		super.onResume();
 	}
 	
 	@Override
 	protected void onPause() {
 		// TODO Auto-generated method stub
 		
 		SharedPreferences pref = getSharedPreferences("setting", MODE_PRIVATE);
 		SharedPreferences.Editor edit = pref.edit();
 		
 		//if(this.edit.getText().toString()) {
 		edit.putString(PREF_USEVALUE, mEdit.getText().toString());
 		//}
 		Log.d("TA", mEdit.getText().toString());
 		
 		if(mChk.isChecked()) {
 			edit.putBoolean("PREF_CHECKBOX", true);
 		} else {
 			edit.putBoolean("PREF_CHECKBOX", false);
 		}
 		edit.commit();
 		
 		super.onPause();
 	}
}
